﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections.Generic;

public partial class Report_PayDetailStatistics : System.Web.UI.Page
{
    private decimal TotalPayAmt
    {
        get
        {
            decimal totalPayAmt = 0.00M;
            if (ViewState["TotalPayAmt"] != null)
            {
                totalPayAmt = Convert.ToDecimal(ViewState["TotalPayAmt"]);
            }
            return totalPayAmt;
        }
        set
        {
            ViewState["TotalPayAmt"] = value;
        }
    }
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    private decimal TotalGetAmt
    {
        get
        {
            decimal totalGetAmt = 0.00M;
            if (ViewState["TotalGetAmt"] != null)
            {
                totalGetAmt = Convert.ToDecimal(ViewState["TotalGetAmt"]);
            }
            return totalGetAmt;
        }
        set
        {
            ViewState["TotalGetAmt"] = value;
        }
    }

    private decimal TotalHRAMT
    {
        get
        {
            decimal totalHRAMT = 0.00M;
            if (ViewState["TotalHRAMT"] != null)
            {
                totalHRAMT = Convert.ToDecimal(ViewState["TotalHRAMT"]);
            }
            return totalHRAMT;
        }
        set
        {
            ViewState["TotalHRAMT"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("900800");
        if (!IsPostBack)
        {
            BindDropPayType();
            BindDropDealType();
        }
    }

    private void BindDropPayType()
    {/*
        List<Model.PAYTYPE> payTypes = new BLL.PAYTYPE().GetAllList();
        dropOrderPayType.DataSource = payTypes;
        dropOrderPayType.DataTextField = "PT_NAME";
        dropOrderPayType.DataValueField = "PT_CODE";
        dropOrderPayType.DataBind();

        dropReturnPayType.DataSource = payTypes;
        dropReturnPayType.DataTextField = "PT_NAME";
        dropReturnPayType.DataValueField = "PT_CODE";
        dropReturnPayType.DataBind();

        dropCZDPayType.DataSource = payTypes;
        dropCZDPayType.DataTextField = "PT_NAME";
        dropCZDPayType.DataValueField = "PT_CODE";
        dropCZDPayType.DataBind();

        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "-1";
        dropOrderPayType.Items.Insert(0, li);
        dropReturnPayType.Items.Insert(0, li);
        dropCZDPayType.Items.Insert(0, li);*/
    }

    private void BindDropDealType()
    {/*
        List<Model.DEALTYPE> properties = new BLL.DEALTYPE().GetAllList();
        dropOrderDealType.DataSource = properties;
        dropOrderDealType.DataTextField = "DT_NAME";
        dropOrderDealType.DataValueField = "DT_CODE";
        dropOrderDealType.DataBind();

        dropReturnDealType.DataSource = properties;
        dropReturnDealType.DataTextField = "DT_NAME";
        dropReturnDealType.DataValueField = "DT_CODE";
        dropReturnDealType.DataBind();

        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "-1";
        dropOrderDealType.Items.Insert(0, li);
        dropReturnDealType.Items.Insert(0, li);
      */
    }
    /// <summary>
    /// DataTable导出Excel
    /// </summary>
    /// <param name="dtData"></param>
    public static void DataTable2Excel(System.Data.DataTable dtData)
    {
        System.Web.UI.WebControls.DataGrid dgExport = null;
        System.Web.HttpContext curContext = System.Web.HttpContext.Current;
        System.IO.StringWriter strWriter = null;
        System.Web.UI.HtmlTextWriter htmlWriter = null;

        if (dtData != null)
        {
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
            curContext.Response.Charset = "";

            strWriter = new System.IO.StringWriter();
            htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

            dtData.Columns.Add("发货/入库");
            dtData.Columns.Add("财务审核");
            dtData.Columns.Add("结算");
            dtData.Columns.Add("到帐"); 

            dtData.Columns["TempID"].ColumnName = "序号";
            dtData.Columns["ID"].ColumnName = "单据编号";
            //dtData.Columns["SendOrReceive"].ColumnName = "发货/入库";
            //dtData.Columns["发货/入库"].DataType = Type.GetType("System.bool");  
            //dtData.Columns["Clear"].ColumnName = "财务审核";
            //dtData.Columns["财务审核"].DataType = Type.GetType("System.bool"); 
            //dtData.Columns["Acheck"].ColumnName = "结算";
            //dtData.Columns["结算"].DataType = Type.GetType("System.bool"); 

            dtData.Columns["CreateDate"].ColumnName = "单据生成时间";
            dtData.Columns["PayTypeName"].ColumnName = "支付方式";
            dtData.Columns["PayAmt"].ColumnName = "支付金额";
            dtData.Columns["PayDate"].ColumnName = "支付日期";
            dtData.Columns["DealId"].ColumnName = "交易号";
            //dtData.Columns["DealDate"].ColumnName = "交易日期";
            dtData.Columns["Account"].ColumnName = "帐户 ";

            //dtData.Columns["Get"].ColumnName = "到帐";
            dtData.Columns["GetAmt"].ColumnName = "到帐金额";
            dtData.Columns["GetDate"].ColumnName = "到帐日期";
            dtData.Columns["DealTypeName"].ColumnName = "交易类型 ";
            dtData.Columns["HRAMT"].ColumnName = "已退货金额";
            dtData.Columns["Note"].ColumnName = "备注";

            for (int i = 0; i < dtData.Rows.Count; i++)
            {
                dtData.Rows[i]["发货/入库"] = dtData.Rows[i]["SendOrReceive"].ToString() == "1" ? "已发货" : "未发货";
                dtData.Rows[i]["财务审核"] = dtData.Rows[i]["Clear"].ToString() == "1" ? "已审核" : "未审核";
                dtData.Rows[i]["结算"] = dtData.Rows[i]["Acheck"].ToString() == "1" ? "已结算" : "未结算";
                dtData.Rows[i]["到帐"] = dtData.Rows[i]["Get"].ToString() == "1" ? "已到账" : "未到账";
            }
            //移除列
            dtData.Columns.Remove(dtData.Columns["Acheck"]);
            dtData.Columns.Remove(dtData.Columns["SendOrReceive"]);
            dtData.Columns.Remove(dtData.Columns["Clear"]);
            dtData.Columns.Remove(dtData.Columns["SendOrReceiveDate"]);
            dtData.Columns.Remove(dtData.Columns["PayType"]);
            dtData.Columns.Remove(dtData.Columns["Get"]);
            dtData.Columns.Remove(dtData.Columns["DealTypeCode"]);
            dtData.Columns.Remove(dtData.Columns["DealDate"]);
            //dtData.Columns.Remove(dtData.Columns["Gift"]);
            //dtData.Columns.Remove(dtData.Columns["Itg"]);

            //dtData.Columns.Remove(dtData.Columns["AddItg"]);
            //dtData.Columns.Remove(dtData.Columns["Offer"]);
            //dtData.Columns.Remove(dtData.Columns["ReturnItg"]);  




            dgExport = new System.Web.UI.WebControls.DataGrid();
            dgExport.DataSource = dtData.DefaultView;
            dgExport.AllowPaging = false;
            dgExport.DataBind();

            dgExport.RenderControl(htmlWriter);
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();
        }
    } 
    private void Bind(Boolean isExport)
    {
        try
        {
            DataSet myds = null;
            this.TotalPayAmt = 0.00M;
            this.TotalGetAmt = 0.00M;
            this.TotalHRAMT = 0.00M;
            if (dropBillType.SelectedValue == "all")
            {
                char[] charTotTrim = new char[] { ',' };
                StringBuilder sbOrderDocWhere = new StringBuilder();
                sbOrderDocWhere.Append(dropDelivery.SelectedValue == "-1" ? "" : " and order_send=" + dropDelivery.SelectedValue + " ");
                sbOrderDocWhere.Append(dropOrderAcheck.SelectedValue == "-1" ? "" : " and ORDER_ACHECK=" + dropOrderAcheck.SelectedValue + " ");
                sbOrderDocWhere.Append(dropOrderClear.SelectedValue == "-1" ? "" : " and ORDER_CLEAR=" + dropOrderClear.SelectedValue + " ");
                sbOrderDocWhere.Append(txtOrderStartDate.Text.Trim() == "" ? "" : " and order_createdate>='" + txtOrderStartDate.Text.Trim() + "' ");
                sbOrderDocWhere.Append(txtOrderEndDate.Text.Trim() == "" ? "" : " and order_createdate<='" + txtOrderEndDate.Text.Trim() + " 23:59:59" + "' ");
                sbOrderDocWhere.Append(dropOrderPayType.Value == "" ? "" : " and PD_PAYTYPE in (select * from dbo.Split('" + dropOrderPayType.Value  + "',','))");
                sbOrderDocWhere.Append(dropOrderDealType.Value == "" ? "" : " and PD_DTCODE in (select * from dbo.Split('" + dropOrderDealType.Value + "',','))");
                sbOrderDocWhere.Append(dropGet.SelectedValue == "-1" ? "" : " and PD_GET=" + dropGet.SelectedValue + " ");
                sbOrderDocWhere.Append(txtOrderPayDateStart.Text.Trim() == "" ? "" : " and PD_PAYDATE>='" + txtOrderPayDateStart.Text.Trim() + "' ");
                sbOrderDocWhere.Append(txtOrderPayDateEnd.Text.Trim() == "" ? "" : " and PD_PAYDATE<='" + txtOrderPayDateEnd.Text.Trim() + " 23:59:59" + "' ");
                sbOrderDocWhere.Append(txtOrderDealDateStart.Text.Trim() == "" ? "" : " and PD_DEALDATE>='" + txtOrderDealDateStart.Text.Trim() + "' ");
                sbOrderDocWhere.Append(txtOrderDealDateEnd.Text.Trim() == "" ? "" : " and PD_DEALDATE<='" + txtOrderDealDateEnd.Text.Trim() + " 23:59:59" + "' ");
                sbOrderDocWhere.Append(txtOrderGetDateStart.Text.Trim() == "" ? "" : " and PD_GETDATE>='" + txtOrderGetDateStart.Text.Trim() + "' ");
                sbOrderDocWhere.Append(txtOrderGetDateEnd.Text.Trim() == "" ? "" : " and PD_GETDATE<='" + txtOrderGetDateEnd.Text.Trim() + " 23:59:59" + "' ");

                StringBuilder sbReturnDocWhere = new StringBuilder();
                sbReturnDocWhere.Append(dropIntoStock.SelectedValue == "-1" ? "" : " and RD_CHECK=" + dropIntoStock.SelectedValue + " ");
                sbReturnDocWhere.Append(dropReturnAcheck.SelectedValue == "-1" ? "" : " and RD_ACHECK=" + dropReturnAcheck.SelectedValue + " ");
                sbReturnDocWhere.Append(dropReturnClear.SelectedValue == "-1" ? "" : " and RD_CLEAR=" + dropReturnClear.SelectedValue + " ");
                sbReturnDocWhere.Append(txtReturnStartDate.Text.Trim() == "" ? "" : " and RD_CREATEDATE>='" + txtReturnStartDate.Text.Trim() + "' ");
                sbReturnDocWhere.Append(txtReturnEndDate.Text.Trim() == "" ? "" : " and RD_CREATEDATE<='" + txtReturnEndDate.Text.Trim() + " 23:59:59" + "' ");
                sbReturnDocWhere.Append(dropReturnPayType.Value == "" ? "" : " and RDPD_PAYTYPE in (select * from dbo.Split('" + dropReturnPayType.Value + "',','))");
                sbReturnDocWhere.Append(dropReturnDealType.Value == "" ? "" : " and RDPD_DTCODE in (select * from dbo.Split('" + dropReturnDealType.Value + "',','))");
                sbReturnDocWhere.Append(txtReturnPayDateStart.Text.Trim() == "" ? "" : " and RDPD_PAYDATE>='" + txtReturnPayDateStart.Text.Trim() + "' ");
                sbReturnDocWhere.Append(txtReturnPayDateEnd.Text.Trim() == "" ? "" : " and RDPD_PAYDATE<='" + txtReturnPayDateEnd.Text.Trim() + " 23:59:59" + "' ");
                sbReturnDocWhere.Append(txtReturnDealDateStart.Text.Trim() == "" ? "" : " and RDPD_DEALDATE>='" + txtReturnDealDateStart.Text.Trim() + "' ");
                sbReturnDocWhere.Append(txtReturnDealDateEnd.Text.Trim() == "" ? "" : " and RDPD_DEALDATE<='" + txtReturnDealDateEnd.Text.Trim() + " 23:59:59" + "' ");

                StringBuilder sbRemittanceWhere = new StringBuilder();
                sbRemittanceWhere.Append(dropCZDPayType.Value == "" ? "" : " and R_POSTMETHOD in (select * from dbo.Split('" + dropCZDPayType.Value + "',','))");
                sbRemittanceWhere.Append(dropCZDCheck.SelectedValue == "-1" ? "" : " and R_VALID=" + dropCZDCheck.SelectedValue + " ");
                sbRemittanceWhere.Append(txtCZDCreateDateStart.Text.Trim() == "" ? "" : " and R_ADDTIME>='" + txtCZDCreateDateStart.Text.Trim() + "' ");
                sbRemittanceWhere.Append(txtCZDCreateDateEnd.Text.Trim() == "" ? "" : " and R_ADDTIME<='" + txtCZDCreateDateEnd.Text.Trim() + " 23:59:59" + "' ");
                sbRemittanceWhere.Append(txtCZDCZDateStart.Text.Trim() == "" ? "" : " and R_CREATETIME>='" + txtCZDCZDateStart.Text.Trim() + "' ");
                sbRemittanceWhere.Append(txtCZDCZDateEnd.Text.Trim() == "" ? "" : " and R_CREATETIME<='" + txtCZDCZDateEnd.Text.Trim() + " 23:59:59" + "' ");
                sbRemittanceWhere.Append(txtCZDCheckDateStart.Text.Trim() == "" ? "" : " and R_VALIDTIME>='" + txtCZDCheckDateStart.Text.Trim() + "' ");
                sbRemittanceWhere.Append(txtCZDCheckDateEnd.Text.Trim() == "" ? "" : " and R_VALIDTIME<='" + txtCZDCheckDateEnd.Text.Trim() + " 23:59:59" + "' ");

                myds = new BLL.PAYDETAIL().GetPayDetailsStatistics("all", sbOrderDocWhere.ToString(), sbReturnDocWhere.ToString(), sbRemittanceWhere.ToString());

                dlistPayDetail.DataSource = myds;
                
                dlistPayDetail.DataBind();
            }
            else if (dropBillType.SelectedValue == "order")
            {
                char[] charTotTrim = new char[] { ',' };
                StringBuilder sbOrderDocWhere = new StringBuilder();
                sbOrderDocWhere.Append(dropDelivery.SelectedValue == "-1" ? "" : " and order_send=" + dropDelivery.SelectedValue + " ");
                sbOrderDocWhere.Append(dropOrderAcheck.SelectedValue == "-1" ? "" : " and ORDER_ACHECK=" + dropOrderAcheck.SelectedValue + " ");
                sbOrderDocWhere.Append(dropOrderClear.SelectedValue == "-1" ? "" : " and ORDER_CLEAR=" + dropOrderClear.SelectedValue + " ");
                sbOrderDocWhere.Append(txtOrderStartDate.Text.Trim() == "" ? "" : " and order_createdate>='" + txtOrderStartDate.Text.Trim() + "' ");
                sbOrderDocWhere.Append(txtOrderEndDate.Text.Trim() == "" ? "" : " and order_createdate<='" + txtOrderEndDate.Text.Trim() + " 23:59:59" + "' ");
                sbOrderDocWhere.Append(dropOrderPayType.Value == "" ? "" : " and PD_PAYTYPE in (select * from dbo.Split('" + dropOrderPayType.Value + "',','))");
                sbOrderDocWhere.Append(dropOrderDealType.Value == "" ? "" : " and PD_DTCODE in (select * from dbo.Split('" + dropOrderDealType.Value + "',','))");
                sbOrderDocWhere.Append(dropGet.SelectedValue == "-1" ? "" : " and PD_GET=" + dropGet.SelectedValue + " ");
                sbOrderDocWhere.Append(txtOrderPayDateStart.Text.Trim() == "" ? "" : " and PD_PAYDATE>='" + txtOrderPayDateStart.Text.Trim() + "' ");
                sbOrderDocWhere.Append(txtOrderPayDateEnd.Text.Trim() == "" ? "" : " and PD_PAYDATE<='" + txtOrderPayDateEnd.Text.Trim() + " 23:59:59" + "' ");
                sbOrderDocWhere.Append(txtOrderDealDateStart.Text.Trim() == "" ? "" : " and PD_DEALDATE>='" + txtOrderDealDateStart.Text.Trim() + "' ");
                sbOrderDocWhere.Append(txtOrderDealDateEnd.Text.Trim() == "" ? "" : " and PD_DEALDATE<='" + txtOrderDealDateEnd.Text.Trim() + " 23:59:59" + "' ");
                sbOrderDocWhere.Append(txtOrderGetDateStart.Text.Trim() == "" ? "" : " and PD_GETDATE>='" + txtOrderGetDateStart.Text.Trim() + "' ");
                sbOrderDocWhere.Append(txtOrderGetDateEnd.Text.Trim() == "" ? "" : " and PD_GETDATE<='" + txtOrderGetDateEnd.Text.Trim() + " 23:59:59" + "' ");


                myds = new BLL.PAYDETAIL().GetPayDetailsStatistics("order", sbOrderDocWhere.ToString(), "","");
                dlistPayDetail.DataSource = myds;
                dlistPayDetail.DataBind();
            }
            else if (dropBillType.SelectedValue == "return")
            {
                StringBuilder sbReturnDocWhere = new StringBuilder();
                sbReturnDocWhere.Append(dropIntoStock.SelectedValue == "-1" ? "" : " and RD_CHECK=" + dropIntoStock.SelectedValue + " ");
                sbReturnDocWhere.Append(dropReturnAcheck.SelectedValue == "-1" ? "" : " and RD_ACHECK=" + dropReturnAcheck.SelectedValue + " ");
                sbReturnDocWhere.Append(dropReturnClear.SelectedValue == "-1" ? "" : " and RD_CLEAR=" + dropReturnClear.SelectedValue + " ");
                sbReturnDocWhere.Append(txtReturnStartDate.Text.Trim() == "" ? "" : " and RD_CREATEDATE>='" + txtReturnStartDate.Text.Trim() + "' ");
                sbReturnDocWhere.Append(txtReturnEndDate.Text.Trim() == "" ? "" : " and RD_CREATEDATE<='" + txtReturnEndDate.Text.Trim() + " 23:59:59" + "' ");
                sbReturnDocWhere.Append(dropReturnPayType.Value == "" ? "" : " and RDPD_PAYTYPE in (select * from dbo.Split('" + dropReturnPayType.Value + "',','))");
                sbReturnDocWhere.Append(dropReturnDealType.Value == "" ? "" : " and RDPD_DTCODE in (select * from dbo.Split('" + dropReturnDealType.Value + "',','))");
                sbReturnDocWhere.Append(txtReturnPayDateStart.Text.Trim() == "" ? "" : " and RDPD_PAYDATE>='" + txtReturnPayDateStart.Text.Trim() + "' ");
                sbReturnDocWhere.Append(txtReturnPayDateEnd.Text.Trim() == "" ? "" : " and RDPD_PAYDATE<='" + txtReturnPayDateEnd.Text.Trim() + " 23:59:59" + "' ");
                sbReturnDocWhere.Append(txtReturnDealDateStart.Text.Trim() == "" ? "" : " and RDPD_DEALDATE>='" + txtReturnDealDateStart.Text.Trim() + "' ");
                sbReturnDocWhere.Append(txtReturnDealDateEnd.Text.Trim() == "" ? "" : " and RDPD_DEALDATE<='" + txtReturnDealDateEnd.Text.Trim() + " 23:59:59" + "' ");

                myds = new BLL.PAYDETAIL().GetPayDetailsStatistics("return", "", sbReturnDocWhere.ToString(),"");
                dlistPayDetail.DataSource=myds ;
                dlistPayDetail.DataBind();  
            }
            else if (dropBillType.SelectedValue == "remittance")
            {
                StringBuilder sbRemittanceWhere = new StringBuilder();
                sbRemittanceWhere.Append(dropCZDPayType.Value == "" ? "" : " and R_POSTMETHOD in (select * from dbo.Split('" + dropCZDPayType.Value + "',','))");
                sbRemittanceWhere.Append(dropCZDCheck.SelectedValue == "-1" ? "" : " and R_VALID=" + dropCZDCheck.SelectedValue + " ");
                sbRemittanceWhere.Append(txtCZDCreateDateStart.Text.Trim() == "" ? "" : " and R_ADDTIME>='" + txtCZDCreateDateStart.Text.Trim() + "' ");
                sbRemittanceWhere.Append(txtCZDCreateDateEnd.Text.Trim() == "" ? "" : " and R_ADDTIME<='" + txtCZDCreateDateEnd.Text.Trim() + " 23:59:59" + "' ");
                sbRemittanceWhere.Append(txtCZDCZDateStart.Text.Trim() == "" ? "" : " and R_CREATETIME>='" + txtCZDCZDateStart.Text.Trim() + "' ");
                sbRemittanceWhere.Append(txtCZDCZDateEnd.Text.Trim() == "" ? "" : " and R_CREATETIME<='" + txtCZDCZDateEnd.Text.Trim() + " 23:59:59" + "' ");
                sbRemittanceWhere.Append(txtCZDCheckDateStart.Text.Trim() == "" ? "" : " and R_VALIDTIME>='" + txtCZDCheckDateStart.Text.Trim() + "' ");
                sbRemittanceWhere.Append(txtCZDCheckDateEnd.Text.Trim() == "" ? "" : " and R_VALIDTIME<='" + txtCZDCheckDateEnd.Text.Trim() + " 23:59:59" + "' ");

                myds = new BLL.PAYDETAIL().GetPayDetailsStatistics("remittance", "","",sbRemittanceWhere.ToString());
                dlistPayDetail.DataSource = myds;
                dlistPayDetail.DataBind(); 
            }
            if (isExport) DataTable2Excel(myds.Tables[0]);
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            Share.Alert(this.Page, "日期输入错误，请输入有效的日期！");
        }
        catch (Exception ex)
        {
            Share.Alert(this.Page,ex.Message);
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Bind(false);
    }
    protected void Export_Click(object sender, EventArgs e)
    {
        Bind(true );
    }
    
    protected void dlistPayDetail_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            this.TotalPayAmt += ((Label)e.Item.FindControl("lblPayAmt")).Text == "" ? 0.00M : Convert.ToDecimal(((Label)e.Item.FindControl("lblPayAmt")).Text);
            this.TotalGetAmt += ((Label)e.Item.FindControl("lblGetAmt")).Text == "" ? 0.00M : Convert.ToDecimal(((Label)e.Item.FindControl("lblGetAmt")).Text);
            this.TotalHRAMT += ((Label)e.Item.FindControl("lblHRAMT")).Text == "" ? 0.00M : Convert.ToDecimal(((Label)e.Item.FindControl("lblHRAMT")).Text);
        }
        else if (e.Item.ItemType == ListItemType.Footer)
        {
            ((Label)e.Item.FindControl("lblPayAmt")).Text = string.Format("{0:0.00}", this.TotalPayAmt);
            ((Label)e.Item.FindControl("lblGetAmt")).Text = string.Format("{0:0.00}", this.TotalGetAmt);
            ((Label)e.Item.FindControl("lblHRAMT")).Text = string.Format("{0:0.00}", this.TotalHRAMT);

        }
    }
}
